package com.ruicar.afs.cloud.afscase.risk.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.risk.entity.CaseTortoiseApply;
import com.ruicar.afs.cloud.afscase.risk.entity.CaseTortoiseDecisionHand;
import com.ruicar.afs.cloud.afscase.risk.service.CaseTortoiseApplyService;
import com.ruicar.afs.cloud.afscase.risk.service.CaseTortoiseDecisionHandService;
import com.ruicar.afs.cloud.afscase.risk.service.CaseTortoiseService;
import com.ruicar.afs.cloud.afscase.risk.vo.DecisionHandInvokeParam;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.Optional;

/**
 * @Description
 * @Author Peng Jinbo
 * @Date 2020/08/17 21:03
 */
@RequestMapping("/decisionInvoke")
@RestController
@Slf4j
@AllArgsConstructor
public class DecisionHandInvokeController {

    /**
     * Case tortoise decision hand service
     */
    private CaseTortoiseDecisionHandService caseTortoiseDecisionHandService;

    /**
     * Case tortoise apply service
     */
    private CaseTortoiseApplyService caseTortoiseApplyService;

    /**
     * Case tortoise service
     */
    private CaseTortoiseService caseTortoiseService;


    /**
     * Author Peng Jinbo
     * Decision hand invoke response
     *
     * @param param param
     * @return the response
     */
    @PostMapping("/handInvoke")
    public IResponse decisionHandInvoke(@RequestBody DecisionHandInvokeParam param){
        caseTortoiseService.signalDecisionHand(param.getApplyNo());
        return IResponse.success("操作成功");
    }

    /**
     * Author Peng Jinbo
     * Get newest hand invoke date str response
     * 获取决策引擎最新调用时间
     *
     * @param param param
     * @return the response
     */
    @GetMapping("/newestDateStr")
    public IResponse getNewestHandInvokeDateStr(@ModelAttribute DecisionHandInvokeParam param){
        if(Objects.isNull(param) || StrUtil.isBlank(param.getApplyNo())){
            return IResponse.success("");
        }else {
            Optional<CaseTortoiseDecisionHand> decisionHandOptional = caseTortoiseDecisionHandService.list(
                    Wrappers.<CaseTortoiseDecisionHand>lambdaQuery()
                            .eq(CaseTortoiseDecisionHand::getApplyNo,param.getApplyNo())
                            .orderByDesc(CaseTortoiseDecisionHand::getOperateDate)
            ).stream().findFirst();

            Optional<CaseTortoiseApply> applyOptional = caseTortoiseApplyService.list(
                    Wrappers.<CaseTortoiseApply>lambdaQuery()
                        .eq(CaseTortoiseApply::getApplyNo,param.getApplyNo())
                        .orderByDesc(CaseTortoiseApply::getOperateDate)
            ).stream().findFirst();

            final String dateFormatStr = "yyyy-MM-dd HH:mm:ss";
            String newestInvokeDateStr = "";
            if(applyOptional.isPresent()){
                newestInvokeDateStr = DateUtil.format(applyOptional.get().getOperateDate(),dateFormatStr);
            }

            if(decisionHandOptional.isPresent()){
                newestInvokeDateStr = DateUtil.format(decisionHandOptional.get().getOperateDate(),dateFormatStr);
            }

            if(decisionHandOptional.isPresent() && applyOptional.isPresent()){
                CaseTortoiseDecisionHand decisionHand = decisionHandOptional.get();
                CaseTortoiseApply apply = applyOptional.get();
                if(Objects.isNull(decisionHand.getOperateDate())){
                    newestInvokeDateStr = DateUtil.format(apply.getOperateDate(), dateFormatStr);
                }

                if(Objects.isNull(apply.getOperateDate())){
                    newestInvokeDateStr = DateUtil.format(decisionHand.getOperateDate(), dateFormatStr);
                }

                if(Objects.nonNull(decisionHand.getOperateDate()) && Objects.nonNull(apply.getOperateDate())){
                    if(decisionHand.getOperateDate().compareTo(apply.getOperateDate()) == 1){
                        newestInvokeDateStr = DateUtil.format(decisionHand.getOperateDate(), dateFormatStr);
                    }else{
                        newestInvokeDateStr = DateUtil.format(apply.getOperateDate(), dateFormatStr);
                    }
                }
            }
            return IResponse.success(newestInvokeDateStr);
        }
    }

}
